<?php

namespace app\modules\Admin\models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use memberModel;

class membertransferlogModel extends Model
{
    protected $table = 'member_transfer_log';
    protected $primaryKey = 'id';
    protected $appends = ['wallet_type_name', 'oper_type_text', 'type_text'];

    protected static array $walletTypes = [
        'money'=>'余额',
        'score'=>'积分',
    ];

    protected static array $types = [
        'rollout'=>'转出',
        'rollin'=>'转入',
    ];

    protected static array $operTypes = [
        'admin'=>'管理员',
        'system'=>'系统',
        'user'=>'用户',
    ];

    /**
     * @param DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * @return string
     */
    public function getWalletTypeNameAttribute()
    {
        $wallet_type_name = '';
        $wallet_type_key = array_keys(self::$walletTypes);
        if(in_array($this->attributes['wallet_type'], $wallet_type_key)){
            $wallet_type_name = self::$walletTypes[$this->attributes['wallet_type']];
        }
        unset($wallet_type_key);
        return $wallet_type_name;
    }

    /**
     * @return string
     */
    public function getOperTypeTextAttribute()
    {
        $oper_type_text = '';
        $oper_type_key = array_keys(self::$operTypes);
        if(in_array($this->attributes['oper_type'], $oper_type_key)){
            $oper_type_text = self::$operTypes[$this->attributes['oper_type']];
        }
        unset($oper_type_key);
        return $oper_type_text;
    }

    /**
     * @return string
     */
    public function getTypeTextAttribute()
    {
        $type_text = '';
        $type_key = array_keys(self::$types);
        if(in_array($this->attributes['type'], $type_key)){
            $type_text = self::$types[$this->attributes['type']];
        }
        unset($type_key);
        return $type_text;
    }

    public static function getRollType(): array
    {
        return self::$types;
    }

    public static function getWalletType(): array
    {
        return self::$walletTypes;
    }

    public function getMember()
    {
        return $this->hasOne(memberModel::class, 'id', 'member_id')->select(['id', 'phone', 'nickname', 'realname', 'avatar']);
    }

    public function getTarget()
    {
        return $this->hasOne(memberModel::class, 'id', 'target_id')->select(['id', 'phone', 'nickname', 'realname', 'avatar']);
    }
}